package com.homework

import scala.math.floor


object Beer {

  def spendMoney(money:Int): Int ={
    //初次买啤酒的瓶数
    val beerNum = money / 2

    //瓶盖与空瓶一共换啤酒的瓶数
    val exchangeNum = exchange(beerNum)

    println(s"最终瓶盖和空瓶换了${exchangeNum}瓶")
    beerNum + exchangeNum
  }

  //定义瓶盖与空瓶的个数
  var caps = 0
  var bottles = 0

  def exchange(beerNum: Int): Int = {

    //有多少瓶啤酒就有多少瓶盖和空瓶
    caps += beerNum
    bottles += beerNum

    //println(s"啤酒数：$beerNum,剩余瓶盖数：$caps,剩余空瓶数：$bottles")

    //如果瓶盖小于5并且空瓶小于3，则说明无法再继续换啤酒
    if (caps <5 && bottles <3) return 0

    //定义瓶盖和空瓶能换多少啤酒
    var capsExchange = 0
    var bottleExchange = 0

    //5个瓶盖换一瓶啤酒，对5取余取得剩下的瓶盖数量
    if(caps >= 5){
      capsExchange = caps/5
      caps = caps%5
    }

    //同上
    if(bottles >= 3){
      bottleExchange = bottles/3
      bottles = bottles%3
    }

    //本轮换取的啤酒数量
    val exchangeBeer = capsExchange + bottleExchange
    println(s"啤酒数：$beerNum，瓶盖和空瓶一共换了${exchangeBeer}瓶酒")

    //进行递归计算，将本轮换取的啤酒数量传入参数
    exchangeBeer + exchange(exchangeBeer)
  }

  def main(args: Array[String]): Unit = {
    println(spendMoney(100))
  }
}
